package com.smartalarmclock.UI.Games;

import java.util.Random;

import com.smartalarmclock.UI.R;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MathExerciseActivity extends Activity{
	
	private TextView mathExerciseText;
	private Button buttonStopAlarm;
	private EditText result;
	private int firstNum = 0;
	private int secondNum = 0;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.game_math_exercise);
		
			Random random = new Random();
			firstNum = random.nextInt(999-0);
			secondNum = random.nextInt(999-0);
	
			/*
			AlertDialog.Builder alert = new AlertDialog.Builder(this);
	
			alert.setTitle("Math Challenge");
			alert.setMessage(firstNum + "   +   " + secondNum + "   =   ");
	
			// Set an EditText view to get user input 
			final EditText input = new EditText(this);
			alert.setView(input);
	
			alert.setNeutralButton("Stop Alarm", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				int res = 0;
				Editable value = input.getText();
				
				try {
					res = Integer.parseInt(value.toString());
				} catch(NumberFormatException nfe) {
					   Log.v("Math Exercise Activity", "Could not parse string to int");
					} 
				
				if(firstNum + secondNum == res)
				{
					exitMathChallenge = true;
					String msgToUser = "You success with the math equation\n Have a nice day";
					Toast.makeText(MathExerciseActivity.this, "You success with the math equation", Toast.LENGTH_LONG).show();
				}
				
				else
				{
					String msgToUser = "You fail with the math equation\n Please try again";
					Toast.makeText(MathExerciseActivity.this, msgToUser, Toast.LENGTH_LONG).show();
				}
					
			  }
			});
			
				alert.show();
				*/
		
		addListenerOnButton();
	}

	private void addListenerOnButton() {
		mathExerciseText = (TextView) findViewById(R.id.textViewMtahExercise);
		buttonStopAlarm = (Button) findViewById(R.id.buttonStopAlarmOnMathChallenge);
		result = (EditText) findViewById(R.id.editTextResOnMathChallenge);
		
		mathExerciseText.setText(String.valueOf(firstNum) + "  +  " + String.valueOf(secondNum) + "  =");
		
		buttonStopAlarm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {			
				int res = 0;
				Editable value = result.getText();
				
				try {
					res = Integer.parseInt(value.toString());
				} catch(NumberFormatException nfe) {
					   Log.v("Math Exercise Activity", nfe.getMessage());
					} 
				
				if(firstNum + secondNum == res)
				{
					String msgToUser = "You success with the math equation\n Have a nice day";
					Toast.makeText(MathExerciseActivity.this, "msgToUser", Toast.LENGTH_LONG).show();
					finish();
				}
				
				else
				{
					String msgToUser = "You fail with the math equation\n Please try again";
					Toast.makeText(MathExerciseActivity.this, msgToUser, Toast.LENGTH_LONG).show();
				}
					
			  }
			});
	}
}
